import { Component, OnInit } from '@angular/core';
import {CarouselService} from '../../../services/carousel.service';
import {Carousel} from '../../../model/Carousel';
import {ToastService} from '../../../services/toast.service';
import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
import {CarouseleditComponent} from '../carouseledit/carouseledit.component';
import {ConfigService} from '../../../services/config.service';

@Component({
  selector: 'app-carousellist',
  templateUrl: './carousellist.component.html',
  styleUrls: ['./carousellist.component.css']
})
export class CarousellistComponent implements OnInit {
  cl:Array<Carousel>;

  constructor(private carouselservice:CarouselService
  ,private toastservice:ToastService
  ,private modalService: NgbModal
  ,public config:ConfigService) {
    this.cl=new Array();
  }

  ngOnInit(): void {
    this.loadCarousel();
  }

  loadCarousel(){
    this.carouselservice.getCarouselList()
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.cl=new Array();
          this.cl=data.list;
        }
        else if(data.msg=='empty'){
          this.cl=new Array();
        }
        else {
          this.toastservice.showError('获取数据错误');
        }

      })
  }

  delete(id:string){
    this.carouselservice.deleteCarousel(id)
      .then((data:any)=>{
        if(data.msg=='ok'){
          this.loadCarousel();
        }
        else {
          this.toastservice.showError('删除异常，请重试');
        }
      })
  }


  add(){
    const modalRef = this.modalService.open(CarouseleditComponent);
    modalRef.componentInstance.id = 'add';

    modalRef.result
      .then((data:any)=>{
        console.dir(data);
        if(data=='saved'){
          this.loadCarousel();
        }
      })
  }

update(id:string){
  const modalRef = this.modalService.open(CarouseleditComponent);
  modalRef.componentInstance.id=id;

  modalRef.result
    .then((data:any)=>{
      console.dir(data);
      if(data=='saved'){
        this.loadCarousel();
      }
    })
}

}
